window.utils = (function() {
	Date.prototype.format = function(format) {
		if (!format) {
			format = this.fullPattern || "yyyy-MM-dd HH:mm:ss";
		}
		var o = {
			"M+": this.getMonth() + 1, // month
			"d+": this.getDate(), // day
			"H+": this.getHours(), // hour (24)
			"h+": this.getHours() % 12, // hour (12)
			"m+": this.getMinutes(), // minute
			"s+": this.getSeconds(), // second
			"q+": Math.floor((this.getMonth() + 3) / 3), // quarter
			"S": this.getMilliseconds()
		};
		if (/(y+)/.test(format)) {
			format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
		}
		for (var k in o) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
			}
		}
		return format;
	};

	function getTimeInterval(Interval, fmt) {
		return Array.apply(null, new Array(Interval))
			.map((v, i) => {
				tmp = new Date();
				switch (fmt) {
					case "year":
						tmp.setFullYear(tmp.getFullYear() - i - 1);
						ret = tmp.format("yyyy年");
						break;
					case "month":
						tmp.setMonth(tmp.getMonth() - i - 1);
						ret = tmp.format("yyyy年MM月");
						break;
					case "day":
						tmp.setDate(tmp.getDate() - i - 1);
						ret = tmp.format("MM月dd日");
						break;
					case "hour":
						tmp.setHours(tmp.getHours() - i - 1);
						ret = tmp.format("dd日HH时");
						break;
				}
				if (!ret) {
					ret = tmp.format("yyyy年MM月dd日 HH时mm分ss秒");
				}
				return ret;
			}).reverse();
	}

	function getNumInterval(Interval, std, vary) {
		return Array.apply(null, new Array(Interval))
			.map((v, i) => {
				return Math.round(std + vary * (Math.random() - 0.5));
			});
	}

	function getFloatInterval(Interval, std, vary) {
		return Array.apply(null, new Array(Interval))
			.map((v, i) => {
				return Math.round((std + vary * (Math.random() - 0.5)) * 100) / 100;
			});
	}

	function getDateTime(fmt) {
		return new Date().format(fmt);
	}

	function calcCurNum(totalNum, fix) {
		curTime = new Date();
		totalSeconds = curTime.getHours() * 60 * 60 + curTime.getMinutes() * 60 + curTime.getSeconds();
		return (totalNum * (totalSeconds / (24 * 60 * 60))).toFixed(fix);
	}
	function calcCurTeamNum(totalNum, fix) {
		curTime = new Date();
		var hour = curTime.getHours();
		if(hour>=16){
			hour -=16;
		}else if(hour>=8){
			hour -=8;
		}
		totalSeconds = hour * 60 * 60 + curTime.getMinutes() * 60 + curTime.getSeconds();
		return (totalNum * (totalSeconds / (24 * 60 * 60))).toFixed(fix);
	}
	function openRTDashboard() {
		var e = document.createElement("a");
		e.href = 'RTDashboard://';
		e.innerHTML = "";
		document.getElementsByTagName("body")[0].appendChild(e);
		e.click();
		document.getElementsByTagName("body")[0].removeChild(e);
	}
	return {
		GetTimeInterval: getTimeInterval,
		GetNumInterval: getNumInterval,
		GetDateTime: getDateTime,
		GetFloatInterval: getFloatInterval,
		CalcCurNum: calcCurNum,
		CalcCurTeamNum: calcCurTeamNum,
		OpenRTDashboard: openRTDashboard
	}
})();
var api = {};
(function() {
	var Cookie = {};
    Cookie.DelCookie = function(name) {
        var expdate = new Date();
        expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
        Cookie.SetCookie(name, "", expdate);
    }
    Cookie.GetCookie = function(name) {
        var search = name + "=";
        var retvalue = "";
        if (document.cookie.length > 0) {
            offset = document.cookie.indexOf(search);
            if (offset != -1) {
                offset += search.length;
                end = document.cookie.indexOf(";", offset);
                if (end == -1)
                    end = document.cookie.length;
                retvalue = unescape(document.cookie.substring(offset, end));
            }
        }
        return retvalue;
    }
    Cookie.SetCookie = function(name, value, expires) {
        var LargeExpDate = new Date();
        if (expires != null) {
            LargeExpDate.setTime(LargeExpDate.getTime() +
                (expires * 1000 * 3600 * 24));
        }
        document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + LargeExpDate.toGMTString()));
    }
    api.Cookie = Cookie;
	api.PostData = function(interface, data, callback, info) {
		$.ajax({
			type: 'POST',
//			url: 'http://' + window.location.hostname + ':81/api/mes/PostData',		
			url: 'http://171.188.96.186:81/api/mes/PostData',
			data: JSON.stringify({
				MsgID: 'mes_' + Math.round(Math.random() * 100000),
				Interface: interface,
				Data: data,
				Info: info
			}),
			dataType: 'json',
			contentType: 'application/json',
			headers: {
				'accessToken': Cookie.GetCookie('accessToken')
			},
			success: function(result, textStatus) {
				if (callback) {
					if (textStatus == "success") {
						callback(result);
					}
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {

			},
			complete: function(XMLHttpRequest, textStatus) {

			}
		})
	}
	api.GetSmartEV = function(callback) {
		$.ajax({
			type: 'GET',
			url: 'http://' + window.location.hostname + ':81/api/ev/getsmartev',
			dataType: 'json',
			contentType: 'application/json',
			success: function(result, textStatus) {
				if (callback) {
					if (textStatus == "success") {
						callback(result);
					}
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {

			},
			complete: function(XMLHttpRequest, textStatus) {

			}
		})
	}
	api.GetIndexEnergyInfo = function(callback) {
        api.PostData('GetIndexEnergyInfo', '', callback, '获取新能源数据情况');
	}
    api.GetYiedBoardInfo = function(callback) {
        api.PostData('GetYiedBoardInfo', '', callback, '获取综合看板生产信息');
    }
    api.GetCurProdStatus = function(callback) {
        api.PostData('GetCurProdStatus', '', callback, '获取硫化看板生产信息');
    }
    api.GetMacStopInfo = function(callback) {
        api.PostData('GetMacStopInfo', '', callback, '获取硫化看板设备停台信息');
    }
    api.GetPowerData = function(callback) {
        api.PostData('GetPowerData', '', callback, '获取动力介质情况');
    }
    api.GetQualityInfo = function(callback) {
        api.PostData('GetQualityInfo', '', callback, '获取动力介质情况');
    }
    api.GetFeedChecked = function(callback) {
        api.PostData('GetFeedChecked', '', callback, '获取上料情况');
    }
})();